package org.pi.common.geom

class Vector3f {
  private var data = new Array[Float](3)
  
  def apply(x: Int) = data(x)
  def update(x: Int, value: Float) { data(x) = value }
  
  def dotprod(v: Vector3f) = (data(0)*v(0)) + (data(1)*v(1)) + (data(2)*v(2))
  def crossprod(b: Vector3f) = Vector3f(
  	(data(1)*b(2)) - (data(2)*b(1)),
    (data(2)*b(0)) - (data(0)*b(2)),
    (data(0)*b(1)) - (data(1)*b(0))
  )

  def /(value: Float) = Vector3f(this(0)/value, this(1)/value, this(2)/value)
  def *(value: Float) = Vector3f(this(0)*value, this(1)*value, this(2)*value)
  def +(value: Float) = Vector3f(this(0)+value, this(1)+value, this(2)+value)
  def -(value: Float) = Vector3f(this(0)-value, this(1)-value, this(2)-value)
  
  def /(value: Vector3f) = Vector3f(this(0)/value(0), this(1)/value(1), this(2)/value(2))
  def *(value: Vector3f) = Vector3f(this(0)*value(0), this(1)*value(1), this(2)*value(2))
  def +(value: Vector3f) = Vector3f(this(0)+value(0), this(1)+value(1), this(2)+value(2))
  def -(value: Vector3f) = Vector3f(this(0)-value(0), this(1)-value(1), this(2)-value(2))

  def length : Float = Math.sqrt( (data(0)*data(0) + data(1)*data(1) + data(2)*data(2)).doubleValue() ).floatValue()
  def sum() : Float = this(0) + this(1) + this(2)
  def normalize() : Vector3f = this/length

  override def toString() = data.toString
}

object Vector3f {
  def apply(a:Float, b:Float, c:Float) = new Vector3f {
    this(0) = a
    this(1) = b
    this(2) = c
  }
  def apply(a:Vector3f) = new Vector3f {
    this(0) = a(0)
    this(1) = a(1)
    this(2) = a(2)
  }
}

object Vector3fMain {
  def main(args: Array[String]) {
    var v1 = Vector3f(3,4,1)
    var v2 = Vector3f(-1,2,7)
    var v3 = (v1 crossprod v2).normalize
    var v12 = v1 dotprod v2
    println(v1+" crossprod "+v2+" == "+v3)
    println(v1+" dotprod "+v2+" == "+v12)
  }
}
